package cn.zh.npro.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class Login extends  AppCompatActivity implements View.OnClickListener{
    EditText username,pass;
    TextView login;
    private Dialog LDialog;
    MyApplication mapp;
    SharedPreferences sp;
    int log=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        log=getIntent().getIntExtra("log",0);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        mapp= (MyApplication) getApplication();
        sp=mapp.getSp();
        initl();
         }

    public void initl(){
        username=findViewById(R.id.username);
        pass=findViewById(R.id.password);
        login=findViewById(R.id.login);
        login.setOnClickListener(this);
        }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.login:
                LDialog= Loading.createLoadingDialog(this,"登录中...");
                getpost();
                break;
              }
          }



      public void getpost(){
        String usernam=username.getText().toString();
        String password=pass.getText().toString();
          System.out.println("------------->username=="+usernam);
        if (usernam.equals("")||password.equals("")){
            ToastUtil.showLong(Login.this,"账号密码不能为空");
            Loading.closeDialog(LDialog);
            return;
            }
          OkHttpUtils.post(BaseUrl.login)
                  .params("userName",usernam)
                  .params("passWord",password)
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          Loading.closeDialog(LDialog);
                          try {
                              Log.e("yst","xx"+s);
                              JSONObject js=new JSONObject(s);
                              boolean State=js.getBoolean("State");
                              String Msg=js.getString("Msg");
                              if (State){
                                  JSONObject Res=js.getJSONObject("Res");
                                  String UserId=Res.getString("UserId");
                                  String Linkman=Res.getString("Linkman");
                                  String GroupID=Res.getString("GroupID");
                                  SharedPreferences.Editor editor=sp.edit();
                                  editor.putString("uid",UserId);
                                  editor.putString("Linkman",Linkman);
                                  editor.putString("GroupID",GroupID);
                                  editor.putBoolean("isload",true);
                                  editor.commit();
                                 Intent intent=new Intent(Login.this,MainActivity.class);
                                 intent.putExtra("logt","1");
                                 startActivity(intent);
                                 finish();
                              }else {
                                  ToastUtil.showLong(Login.this,Msg);
                              }
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                       }
                  });
               }



    boolean mBackKeyPressed=false;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!mBackKeyPressed) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mBackKeyPressed = true;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mBackKeyPressed = false;
                    }
                },2000);
                return true;
            } else {
                this.finish();
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }




}


